package com.lg.atp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.lg.atp.dao.ParaDao;
import com.lg.atp.dao.ParaDataDao;
import com.lg.atp.entity.TblPara;
import com.lg.atp.entity.TblParaData;
import com.lg.atp.service.IParaService;

@Service("iParaService")
public class ParaServiceImpl implements IParaService{

	@Autowired
	private ParaDao paraDao;
	@Autowired
	private ParaDataDao paraDataDao;
	
	@Override
	public List<TblPara> getSelPara(Map<String,Object> map) {
		return paraDao.getSelData(map);
	}
	
	public Long savePara(TblPara para) {
		 paraDao.savePara(para);
		 return para.getId();
	}

	@Transactional
	@Override
	public Long saveParaData(TblPara para) {
		paraDao.savePara(para);
		List<TblParaData> list = para.getParaDataList();
		Long paraId = para.getId();
		if(list != null && !list.isEmpty()) {
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("list", para.getParaDataList());
			map.put("paraId", paraId);
			paraDataDao.saveParaDatas(map);
		}
		return paraId;
	}

	@Override
	public boolean hasExistsPara(TblPara para) {
		TblPara oldPara = paraDao.selectParaByName(para);
		if(oldPara != null)
			return true;
		return false;
	}

	@Override
	public List<TblPara> getCrPara(Map<String,Object> map) {
		return paraDao.getCrData(map);
	}

	@Override
	public List<TblPara> selectParaByDatasetId(TblPara para) {
		return paraDao.selectParaByDatasetId(para);
	}

	@Override
	public JSONObject selectParaName() {
		
		List<TblPara> list = paraDao.selectParaName();
		JSONObject json = new JSONObject();
		for(TblPara para:list) {
			json.put(para.getId().toString(), para.getParaName());
		}
		return json;
	}

	@Override
	public void setParaDatasetNull(Long datasetId) {

		paraDao.setParaDatasetNull(datasetId);
	}


	public void updateParaDatasetDisable(Long datasetId){
		paraDao.updateParaDatasetDisable(datasetId);
	}

	@Override
	public Long hasParaInDataset(String paraName, Long datasetId) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("paraName", paraName);
		if(datasetId != null)
			map.put("datasetId", datasetId);
		return paraDao.hasParaInDataset(map);
	}

	@Transactional
	@Override
	public void changeParaData(Map<String, Object> map) {

		Long paraId = map.get("paraId") == null?null:Long.valueOf(map.get("paraId").toString());
		String paraData = map.get("paraData") == null?null:map.get("paraData").toString();
		if(paraId != null && StringUtils.isNotBlank(paraData)) {
			List<TblParaData> list = JSONObject.parseArray(paraData, TblParaData.class);
			paraDataDao.delParaDataByParaId(paraId);
			Map<String,Object> sm = new HashMap<String,Object>();
			sm.put("paraId", paraId);
			sm.put("list", list);
			paraDataDao.saveParaDatas(sm);
			
		}
	}

	@Override
	public List<TblParaData> selectParaDataByParaId(Long paraId) {
		List<TblParaData> list = paraDataDao.selectParaDataByParaId(paraId);
		return list;
	}

}
